﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace MonolithHelper
{




    public class ColorPhone
    {

        public static string strUserID;
        public static int intTimeClockAcctID;
        public static string thisExtension;

        public static void LoadDataFromXML(string xml)
        {
            if (string.IsNullOrEmpty(xml))
                xml= "C:\\ColorPhone\\XMLSettings\\SIPSettings.xml";

            if (System.IO.File.Exists(xml))
            {

                XDocument loaded = XDocument.Load(xml);


                IEnumerable<XAttribute> attrb = from a in loaded.Descendants("SIPSettings").Attributes()
                                                select a;
                //   String strUserID = string.Empty;
                //   int intTimeClockAcctID = 0;

                foreach (XAttribute z in attrb)
                {
                    switch (z.Name.ToString())
                    {
                        case "UserID":
                            strUserID = (String)z.Value;
                            break;
                        case "TimeClockAcctID":
                            intTimeClockAcctID = Convert.ToInt32((String)z.Value);
                            break;
                    }

                }


                //---- READ EXTENSION
                IEnumerable<XElement> ExtElement =
                           from el in loaded.Descendants("Extension")
                           select el;

                foreach (XElement e in ExtElement)
                {
                    thisExtension = (String)e.Element("Ext").Value;
                    // strPassword = (String)e.Element("Password").Value;
                }




                //    // READ SERVERS
                //    IEnumerable<XElement> SRVElement =
                //               from l in loaded.Descendants("SipSrv")
                //               select l;


                //    iServer = SRVElement.Count();
                //    AX = new AxPortSIPCoreLibLib.AxPortSIPCoreLib[iServer];
                //    MyServers = new clsServer[iServer];

                //    CreateAXObj();





                //    int i = 0;
                //    foreach (XElement e in SRVElement)
                //    {
                //        //  string Default; //= false;

                //        if (e.Element("DefaultServer").Value == null)
                //        {
                //            MyServers[i].IP = (String)e.Element("serverName").Value;
                //            MyDefaultServer = MyServers[i];
                //        }
                //        else
                //        {
                //            //blDefaultServer = Convert.ToBoolean(e.Element("DefaultServer").Value);
                //            //strServerName = (String)e.Element("servername").Value;                       

                //            if (Convert.ToBoolean(e.Element("DefaultServer").Value) == true)
                //            {
                //                //blDefaultServer = false;
                //                //strServerName = (String)e.Element("servername").Value;
                //                MyServers[i].IP = (String)e.Element("serverName").Value;
                //                MyServers[i].IsDefaul = true;
                //                MyDefaultServer = MyServers[i];
                //            }
                //            else
                //            {
                //                //blDefaultServer = Convert.ToBoolean(e.Element("DefaultServer").Value);
                //                //strServerName = (String)e.Element("servername").Value;
                //                MyServers[i].IP = (String)e.Element("serverName").Value;
                //                MyServers[i].IsDefaul = false;
                //            }
                //        }

                //        i += 1;
                //    }



                //    ColorPhone.Properties.Settings.Default.displayName = intExtension.ToString();
                //    ColorPhone.Properties.Settings.Default.password = strPassword;


                //    ColorPhone.Properties.Settings.Default.Save();

                //    textBoxUsername.Text = intExtension.ToString(); // userName;
                //    textBoxPassword.Text = strPassword; // password;
                //    //textBoxSIPServer.Text = strServerName;
                //    //textBoxSIPServer.Text = strServerDefault;
                //    textBoxServerPort.Text = "";
                //    textBoxDisplayName.Text = "";// displayName ;
                //    textBoxAuthName.Text = intExtension.ToString();// authName;

                //    LoadBlackListNumbersFromXML();
                //}
                //else
                //{
                //    Program.Log.WriteLogToFile(LogType.Errors, "LoadDataFromXML", "No SIP settings found");
                //    MessageBox.Show("No SIP settings found. Try to 'Get Ext Settings' on ColorPhone XML on your icon tray or 'Check Status' on TimeClock.", "ColorPhone", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    Application.Exit();
                //    //  this.grdVw.Rows.Add(new object[] { "10.22.22.221", true });
                //    //  this.grdVw.Rows.Add(new object[] { "10.22.22.45", false });
                //}

            }

        }



    }
}
